<?php

class Role_model extends CI_Model
{

    private $_table = 'sm_role';
    private $_tbl_ruser = 'sm_users_role';

    public function __construct()
    {
        parent::__construct();
    }

    public function getAll($conditions = array())
    {
        if (count($conditions) > 0)
            $this->db->where($conditions);

        $this->db->from($this->_table);
        $this->db->select('*');
        $result = $this->db->get()->result();

        return $result;
    }

    /*
     * Select role for user
     */

    public function getRole($id)
    {
        $this->db->where('role_id', $id);
        $query = $this->db->get($this->_table);
        return $query->row();
    }

    function addRole($insertData = array())
    {
        $result = $this->db->insert($this->_table, $insertData);
        return $result;
    }

    public function updateRole($id, $data)
    {
        $this->db->where('role_id', $id);
        $this->db->update($this->_table, $data);
    }

    /*
     * Select role for user
     */

    public function getRoleUserByID($id)
    {
        $this->db->where('userid', $id);
        $query = $this->db->get($this->_tbl_ruser);
        return $query->row();
    }

    /*
     * Create role for user
     */

    function createRoleUser($insertData = array())
    {
        $result = $this->db->insert($this->_tbl_ruser, $insertData);
        return $result;
    }

    /*
     * Update Role user
     */

    function updateRoleUser($updateKey = array(), $updateData = array())
    {
        $result = $this->db->update($this->_tbl_ruser, $updateData, $updateKey);
        return $result;
    }

    /*
     * Delete Role user
     */

    function deleteRoleUser($conditions = array())
    {
        if (count($conditions) > 0)
            $this->db->where($conditions);
        $result = $this->db->delete($this->_tbl_ruser);
        return $result;
    }

}
